using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Squog_1._1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Controller : Microsoft.Xna.Framework.GameComponent
    {

#if XBOX
        GamePadState PrevState;
        PlayerIndex i;
#else
        KeyboardState PrevState;
#endif

#if XBOX
        public Controller(Game game, PlayerIndex i)
            : base(game)
        {
            this.i = i;
        }
#else
        public Controller(Game game)
            : base(game)
        {
        }
#endif

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
#if XBOX
            PrevState = GamePad.GetState(i);
#else
            PrevState = Keyboard.GetState();
#endif
            base.Initialize();
        }

        public bool SelectOnRelease()
        {
#if XBOX
            return ((PrevState.Buttons.A == ButtonState.Pressed &&  GamePad.GetState(i).Buttons.A == ButtonState.Released) ||
                (PrevState.Buttons.Start == ButtonState.Pressed &&  GamePad.GetState(i).Buttons.Start == ButtonState.Released));
#else
            return (PrevState.IsKeyDown(Keys.Enter) && Keyboard.GetState().IsKeyUp(Keys.Enter));
#endif
        }

        public bool PauseOnRelease()
        {
#if XBOX
            return (PrevState.Buttons.Start == ButtonState.Pressed &&  GamePad.GetState(i).Buttons.Start == ButtonState.Released);
#else
            return (PrevState.IsKeyDown(Keys.P) && Keyboard.GetState().IsKeyUp(Keys.P));
#endif
        }

        //back button is hit
        public bool BackOnRelease()
        {
#if XBOX
#else
            return PrevState.IsKeyDown(Keys.B);
#endif
        }

        public bool UpOnRelease()
        {
#if XBOX
            return (PrevState.ThumbSticks.Left.Y >= 0.1 &&  GamePad.GetState(i).ThumbSticks.Left.Y < 0.1);
#else
            return (PrevState.IsKeyDown(Keys.Up) && Keyboard.GetState().IsKeyUp(Keys.Up));
#endif
        }

        public bool UpOnPress()
        {
#if XBOX
            return (PrevState.ThumbSticks.Left.Y < 0.1 &&  GamePad.GetState(i).ThumbSticks.Left.Y >= 0.1);
#else
            return (PrevState.IsKeyUp(Keys.Up) && Keyboard.GetState().IsKeyDown(Keys.Up));
#endif
        }

        public bool DownOnRelease()
        {
#if XBOX
            return (PrevState.ThumbSticks.Left.Y <= -0.1 &&  GamePad.GetState(i).ThumbSticks.Left.Y > -0.1);
#else
            return (PrevState.IsKeyDown(Keys.Down) && Keyboard.GetState().IsKeyUp(Keys.Down));
#endif
        }

        public bool DownOnPress()
        {
#if XBOX
            return (PrevState.ThumbSticks.Left.Y > -0.1 &&  GamePad.GetState(i).ThumbSticks.Left.Y <= -0.1);
#else
            return (PrevState.IsKeyUp(Keys.Down) && Keyboard.GetState().IsKeyDown(Keys.Down));
#endif
        }

        public Vector2 LDirection()
        {
#if XBOX
            return GamePad.GetState(i).ThumbSticks.Left;
#else
            Vector2 Dir = Vector2.Zero;

            KeyboardState KS = Keyboard.GetState();
            if(KS.IsKeyDown(Keys.Left))
                Dir.X--;
            if(KS.IsKeyDown(Keys.Right))
                Dir.X++;
            if(KS.IsKeyDown(Keys.Up))
                Dir.Y--;
            if(KS.IsKeyDown(Keys.Down))
                Dir.Y++;

            if(Dir != Vector2.Zero)
                Dir.Normalize();
            return Dir;
#endif
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
#if XBOX
            PrevState = GamePad.GetState(i);
#else
            PrevState = Keyboard.GetState();
#endif
            base.Update(gameTime);
        }
    }
}
